package kickboxing.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import kickboxing.controller.UzytkownikManager;
import kickboxing.model.Uzytkownik;


public class RejestracjaAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    public RejestracjaAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		Uzytkownik user = new Uzytkownik();
		UzytkownikManager linkController = new UzytkownikManager();
		
		try {
			BeanUtils.populate(user, request.getParameterMap());
			if(linkController.sprawdzLoginIMail(user.getLogin(), user.getMail()) == true){;
				linkController.rejestruj(user);
				String message = "Pomyslnie zarejestrowano, czekaj na aktywacje konta";
				request.setAttribute("message", message);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/rejestruj2.jsp");
				dispatcher.forward(request, response); 
			}else{
				String message = "Niestety, ale podany login lub mail jest zajety. Prosze podac nowy";
				request.setAttribute("message", message);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/rejestruj2.jsp");
				dispatcher.forward(request, response);
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
